package com.sinodata.bsm.cicp.collector.informix;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * @author xuxiaoxiao
 *
 */
public class IfxShareMemRateCollector extends AbstractJDBCCollector {

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String dbtype = getParamMap().get("DBType");
        String dbname = getParamMap().get("DBName");
        String sql = getParamMap().get("SQL");
        String key = IfxShareMemRateCollector.class.getName() + ":" + ip + ":" + dbname + ":" + ":" + dbtype;
        ResultSet rs = null;
        Long tmp1 = 0L;
        Long tmp2 = 0L;
        Long tmp3 = 0L;
        Long tmp4 = 0L;
        Long tmp5 = 0L;
        Long tmp6 = 0L;
        Long tmp7 = 0L;
        double rrate = 0d;
        double wrate = 0d;
        Long time = System.currentTimeMillis();
        try {
            rs = getCachedResultSet(dbtype, ip, dbname, sql);
            if (rs.next()) {
                rrate = rs.getDouble(1);
                wrate = rs.getDouble(2);
                tmp1 = rs.getLong(3);
                tmp2 = rs.getLong(4);
                tmp3 = rs.getLong(5);
                tmp4 = rs.getLong(6);
                tmp5 = rs.getLong(7);
                tmp6 = rs.getLong(8);
                tmp7 = rs.getLong(9);
            }
            String last_value = (String) getPersistentData(key);
            //将采集到得值进行临时保存，保存目录为 agent/save/..
            savePersistentData(key, tmp1 + "#" + tmp2 + "#" + tmp3 + "#" + tmp4 + "#" + tmp5 + "#" + tmp6 + "#" + tmp7 + "#" + time);
            List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
            String[] propMetas = getPropMeta();
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            propValues[0] = ResPropertyValue.createPropValue(rrate + "", propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(wrate + "", propMetas[1]);
            if (last_value != null && tmp1 >= 0 && tmp2 >= 0 && tmp3 >= 0 && tmp4 >= 0 && tmp5 >= 0 && tmp6 >= 0 && tmp7 >= 0) {
                String tmpvalues[] = last_value.split("#");
                //秒
                Double second = (time - Long.parseLong(tmpvalues[7])) * 1.00 / 1000;

                double rate = 0d;
                if (second != 0)
                    rate = (tmp1 - Long.parseLong(tmpvalues[0])) * 1.00 / second;
                propValues[2] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[2]);

                rate = 0d;
                if (second != 0)
                    rate = (tmp2 - Long.parseLong(tmpvalues[1])) * 1.00 / second;
                propValues[3] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[3]);

                rate = 0d;
                if (second != 0)
                    rate = (tmp3 - Long.parseLong(tmpvalues[2])) * 1.00 / second;
                propValues[4] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[4]);

                rate = 0d;
                if (second != 0)
                    rate = (tmp4 - Long.parseLong(tmpvalues[3])) * 1.00 / second;
                propValues[5] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[5]);

                rate = 0d;
                if (second != 0)
                    rate = (tmp5 - Long.parseLong(tmpvalues[4])) * 1.00 / second;
                propValues[6] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[6]);

                rate = 0d;
                if (second != 0)
                    rate = (tmp6 - Long.parseLong(tmpvalues[5])) * 1.00 / second;
                propValues[7] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[7]);

                rate = 0d;
                if (second != 0)
                    rate = (tmp7 - Long.parseLong(tmpvalues[6])) * 1.00 / second;
                propValues[8] = ResPropertyValue.createPropValue(Math.round(rate > 0 ? rate : 0d) + "", propMetas[8]);

            } else {
                propValues[2] = ResPropertyValue.createPropValue("0", propMetas[2]);
                propValues[3] = ResPropertyValue.createPropValue("0", propMetas[3]);
                propValues[4] = ResPropertyValue.createPropValue("0", propMetas[4]);
                propValues[5] = ResPropertyValue.createPropValue("0", propMetas[5]);
                propValues[6] = ResPropertyValue.createPropValue("0", propMetas[6]);
                propValues[7] = ResPropertyValue.createPropValue("0", propMetas[7]);
                propValues[8] = ResPropertyValue.createPropValue("0", propMetas[8]);

            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;

        } catch (Exception e) {
            logger.error("get informix share memory rate information failed! ", e);
            throw new CollectorException(e);
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                }
        }
    }

    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL" };
    }

    public String[] getPropMeta() {
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" };
    }

}
